package com.foreveross.project.usercenterserver.oauth2.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.foreveross.project.usercenterserver.common.constant.ResultCode;
import com.foreveross.project.usercenterserver.common.utils.R;
import com.foreveross.project.usercenterserver.exception.CustomException;
import com.foreveross.project.usercenterserver.oauth2.domain.UcAccount;
import com.foreveross.project.usercenterserver.oauth2.dto.AccountDto;
import com.foreveross.project.usercenterserver.oauth2.service.IUcAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yz
 * @since 2019-07-22
 */
@Api(description = "人员管理")
@RestController
@RequestMapping("/oauth2/ucAccount")
public class UcAccountController {


    @Autowired
    private IUcAccountService accountService;


    /**
     * 添加人员
     */
    @PostMapping(value = "/createAccount")
    @ApiOperation(value = "新建人员", notes = "成功返回 Ucaccount 实体")
    public R createAccount(
            @Valid @RequestBody AccountDto accountDto) {
        UcAccount account = null;
        try {
            account = accountService.createAccount(accountDto);
            return R.ok(account);
        } catch (CustomException e) {
            return R.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改员工
     */
    @PostMapping(value = "/updateAccount")
    @ApiOperation(value = "修改员工", notes = "成功返回 UcAccount 实体")
    public R updateAccount(
            @Valid @RequestBody AccountDto accountDto) {
        UcAccount account = null;
        try {
            account = accountService.updateAccount(accountDto);
            return R.ok(account);
        } catch (CustomException e) {
            return R.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除员工
     */
    @DeleteMapping(value = "/deleteAccount/{accountFkId}")
    @ApiOperation(value = "删除员工", notes = "成功返回code=200")
    public R deleteAccount(
            @PathVariable String accountFkId
    ) {
        try {
            accountService.deleteAccount(accountFkId);
            return R.ok();
        } catch (Exception e) {
            return R.error(ResultCode.BAD_REQUEST.getCode(), e.getMessage());
        }
    }

    /**
     * 查询单个员工
     *
     * @param accountFkId
     * @return
     */
    @GetMapping(value = "/detail/{accountFkId}")
    @ApiOperation(value = "查询单个员工", notes = "成功返回 UcAccount 实体")
    public R accountDetail(
            @PathVariable String accountFkId
    ) {
        try {
            return R.ok(accountService.findAccount(accountFkId));
        } catch (Exception e) {
            return R.error(ResultCode.BAD_REQUEST.getCode(), e.getMessage());
        }
    }

    /**
     * 应用列表
     */
    @GetMapping(value = "/accountList")
    @ApiOperation(value = "员工列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", dataType = "Integer", defaultValue = "1", required = false, value = "当前页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", dataType = "Integer", defaultValue = "10", required = false, value = "每页记录数", paramType = "query"),
            @ApiImplicitParam(name = "tenantFkId", dataType = "String", required = true, value = "租户FkId", paramType = "query"),
            @ApiImplicitParam(name = "appFkId", dataType = "String", required = true, value = "应用id", paramType = "query"),
            @ApiImplicitParam(name = "username", dataType = "String", required = true, value = "用户名称", paramType = "query"),
            @ApiImplicitParam(name = "nickName", dataType = "String", required = true, value = "用户昵称", paramType = "query"),
            @ApiImplicitParam(name = "email", dataType = "String", required = true, value = "邮件", paramType = "query"),
            @ApiImplicitParam(name = "mobile", dataType = "String", required = true, value = "电话", paramType = "query"),
            @ApiImplicitParam(name = "delFlag", dataType = "Integer", defaultValue = "0", required = false, value = "是否删除  -1：已删除  0：正常", paramType = "query"),
    })
    public R accountList(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = true) String tenantFkId,
            @RequestParam(required = true) String appFkId,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String nickName,
            @RequestParam(required = false) String email,
            @RequestParam(required = false) String mobile,
            @RequestParam(required = false) Integer delFlag
    ) {
        //查询条件
        Map<String, Object> params = new HashMap<>();
        params.put("appFkId", appFkId);
        params.put("username", username);
        params.put("nickName", nickName);
        params.put("email", email);
        params.put("mobile", mobile);
        params.put("tenantFkId", tenantFkId);
        params.put("delFlag", delFlag);
        IPage<AccountDto> dtoIPage = null;
        try {
            dtoIPage = accountService.queryForPage(new Page<>(pageNum, pageSize), params);
            return R.ok(dtoIPage);
        } catch (Exception e) {
            return R.error(ResultCode.BAD_REQUEST.getCode(), e.getMessage());
        }
    }



}
